<?php

namespace App\Models;

use Kalnoy\Nestedset\NodeTrait;
use Tymon\JWTAuth\Contracts\JWTSubject;
use Illuminate\Foundation\Auth\User as Authenticatable;

/**
 * App\Models\User
 *
 * @property int $id
 * @property int $_lft
 * @property int $_rgt
 * @property int|null $parent_id
 * @property string|null $avatar 头像
 * @property string $username 用户名
 * @property string $email 邮箱
 * @property string|null $area 区号
 * @property string|null $mobile 手机号
 * @property string|null $password 登录密码
 * @property string|null $pay_password 支付密码
 * @property int $vip_level 用户等级:0-普通节点 1-初级节点 2-中级节点 3-高级节点
 * @property int $admin_level 管理员指定等级:0-普通节点 1-初级节点 2-中级节点 3-高级节点
 * @property int|null $status 状态:0.正常 1.禁用
 * @property int|null $withdraw_switch 提币黑名单:0.正常 1.禁止提币
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @property-read \Kalnoy\Nestedset\Collection|\App\Models\User[] $children
 * @property-read int|null $children_count
 * @property-read \App\Models\User|null $parent
 * @method static \Kalnoy\Nestedset\Collection|static[] all($columns = ['*'])
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User d()
 * @method static \Kalnoy\Nestedset\Collection|static[] get($columns = ['*'])
 * @method static \Kalnoy\Nestedset\QueryBuilder|\App\Models\User newModelQuery()
 * @method static \Kalnoy\Nestedset\QueryBuilder|\App\Models\User newQuery()
 * @method static \Kalnoy\Nestedset\QueryBuilder|\App\Models\User query()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User whereArea($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User whereAvatar($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User whereEmail($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User whereLft($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User whereMobile($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User whereParentId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User wherePassword($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User wherePayPassword($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User whereRgt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User whereStatus($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User whereUpdatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User whereUsername($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User whereVipLevel($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User whereWithdrawSwitch($value)
 * @mixin \Eloquent
 */
class User extends Authenticatable implements JWTSubject
{
    use NodeTrait;
    public $table = 'users';

    protected $hidden = ['pay_password', 'password'];
    // Rest omitted for brevity

    /**
     * Get the identifier that will be stored in the subject claim of the JWT.
     *
     * @return mixed
     */
    public function getJWTIdentifier()
    {
        return $this->getKey();
    }

    /**
     * Return a key value array, containing any custom claims to be added to the JWT.
     *
     * @return array
     */
    public function getJWTCustomClaims()
    {
        return [];
    }

    /**
     * 统计团队人数（不含自己）.
     * @param User $user
     * @return int
     */
    public static function calTeamNumber(User $user)
    {
        return ($user->_rgt - $user->_lft - 1) / 2;
    }

    /**
     * 统计矿池算力（含自己）.
     * @param User $user 用户
     * @return int
     */
    public static function calTeamOrderNumber(User $user)
    {
        $user_ids = User::where('_lft', '>=', $user->_lft)
            ->where('_rgt', '<=', $user->_rgt)
            ->pluck('id');

        return Device::whereIn('user_id', $user_ids)->sum('use');
    }

    // 直推用户（预购订单 + 订购订单）
    public static function recomendCount($user_id)
    {
        $game = Game::orderByDesc('id')->first();
        // 购买订单
        $u1 = GameOrder::where('game_id', $game->id)->where('parent_id', $user_id)->select('user_id')->distinct()->pluck('user_id')->toArray();
        // 预购订单
        $u2 = GamePreOrder::where('game_id', $game->id)->where('parent_id', $user_id)->select('user_id')->distinct()->pluck('user_id')->toArray();
        $u = array_unique(array_merge($u1, $u2));
        return $u;
    }
}
